package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class AttribSelectionPane extends AdjustablePanel {
    private AttribFlatPane pnlAttribFlat;
    private AttribPercPane pnlAttribPerc;

    public AttribSelectionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAttribFlat)).addGroup(layout.createParallelGroup().addComponent(this.pnlAttribPerc));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAttribFlat).addComponent(this.pnlAttribPerc));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, this.pnlAttribFlat, this.pnlAttribPerc);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_ATTRIBS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlAttribFlat == null) {
            this.pnlAttribFlat = new AttribFlatPane(1);
        } else {
            this.pnlAttribFlat.adjustUI();
        }
        if (this.pnlAttribPerc == null) {
            this.pnlAttribPerc = new AttribPercPane(1);
        } else {
            this.pnlAttribPerc.adjustUI();
        }
    }

    public void clear() {
        this.pnlAttribFlat.clear();
        this.pnlAttribPerc.clear();
    }

    public void addCriteria(final SelectionCriteria criteria) {
        this.pnlAttribFlat.addCriteria(criteria);
        this.pnlAttribPerc.addCriteria(criteria);
    }
}
